package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Person p = new Person();
        p.sayHello();

        Scanner scanner = new Scanner(System.in);
        while (true){
        System.out.println("请输入类名");
        String className = scanner.nextLine();
        System.out.println("请输入方法名");
        String methodNome = scanner.nextLine();
        //实例化
        Class cls = Class.forName(className);
        Object obj = cls.newInstance();//Object obj = new Person();
        //调用方法
        //1获取待调用方法的方法对象
        Method method = cls.getMethod(methodNome);
        //2通过方法对象来调用此方法
        method.invoke(obj);//obj.sayHello();
            }
    }
}
